/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package xib.parserTools.adaptee;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import xib.parserTools.interfaces.IChapterData;
import xib.parserTools.interfaces.IAdapterParser;
import xib.parserTools.interfaces.IHeaderData;

/**
 * Implementation of an adaptee with SAX parser library.
 * @author Jose Miguel
 */
public class AdapteeParserSax implements IAdapterParser
{

    /**
     * The information of the header
     */
    private IHeaderData headerData;

    /**
     * The information of the chapters
     */
    private ArrayList<IChapterData> chaptersData;
    
    /**
     * Default constructor
     */
    public AdapteeParserSax() 
    {
        // TODO: Look for some code here
    }

    /**
     * Open and parse the file located in pathname
     * @param pathname Absolute location of the file to parse
     * @throws IOException If the file dont exist
     */
    public void loadInputStream(InputStream input) throws IOException {
        try {
            parse(input);
        } catch (IOException ex) {
            throw ex;
        }
    }

    public boolean validateFile(String pathname)
    {
        boolean ret = false; 
        // TODO: Code for validate de file
        return ret; 
    }

    
    /**
     * Function to parse the file
     * @param f Input file to parse
     * @throws IOException
     */
    private void parse(InputStream f) throws IOException
    {
        try {
            // Get the SAXParser
            SAXParserFactory spf = SAXParserFactory.newInstance();
            SAXParser sp = spf.newSAXParser();

            // Get the XML reader needed for parse
            XMLReader xr = sp.getXMLReader();
            XibDataHandler xdh = new XibDataHandler();
            xr.setContentHandler(xdh);
            xr.parse(new InputSource(f));

            // Get the resutl data
            headerData = xdh.getHeader();
            chaptersData = xdh.getChaptersData();

        } catch (ParserConfigurationException ex) {
            throw new IOException("Invalid configuration. D: " + ex.getMessage());
        } catch (SAXException ex) {
            // Error if the file is not well formed
            throw new IOException("Invalid file format. D: " + ex.getMessage());
        }
    }

    /**
     * Return the header data from the file
     * @return Object with all the information of the header
     */
    public IHeaderData getHeader() {
        return headerData;
    }

    /**
     * Return the list of chapters data
     * @return A list with the chapters data
     */
    public ArrayList<IChapterData> getChapters() {
        return chaptersData;
        
    }

}
